// generic drawing of more complex things

//
// Ripple keyframes
//

@keyframes ripple {
  to {
    background-size: 1000% 1000%;
  }
}

@keyframes ripple-on-slider {
  to {
    background-size: auto, 1000% 1000%;
  }
}

@keyframes ripple-on-headerbar {
  from {
    background-image: radial-gradient(circle, $primary 0%, transparent 0%);
  }

  to {
    background-image: radial-gradient(circle, $primary 100%, transparent 100%);
  }
}


@mixin entry($t, $fc: $primary) {
//
// entry
//
// $t: entry type
// $fc: focus color
//

  @if $t == normal {
    transition: $transition,
                $transition-outline;
    background-color: if($fc == $primary, $entry, rgba($fc, 0.1));
    color: if($fc == $primary, $text-secondary, rgba($fc, 0.75));
    outline: 0 solid transparent;
    outline-offset: 4px;
  }

  @if $t == hover {
    background-color: $overlay-hover;
    // box-shadow: inset 0 0 0 2px $overlay-hover;
    color: if($fc == $primary, $text, $fc);
  }

  @if $t == focus {
    background-color: $overlay-focus;
    color: if($fc == $primary, $text, $fc);
    outline: 2px solid if($fc == $primary, $track, $fc);
    outline-offset: -2px;
  }

  @if $t == checked {
    background-color: if($fc == $primary, $entry, rgba($fc, 0.1));
    color: if($fc == $primary, $text, $fc);
    outline: 2px solid if($fc == $primary, $primary, $fc);
    outline-offset: -2px;
  }

  @if $t == disabled {
    box-shadow: inset 0 0 0 2px transparent;
    background-color: if($fc == $primary, $entry, rgba($fc, 0.1));
    color: if($fc == $primary, $text-disabled, rgba($fc, 0.35));
    outline: none;
  }
}


@mixin button($t) {
//
// button
//
// $t: button type
//

  @if $t == normal {
    transition: $transition,
                background-size 0ms,
                background-image 0ms,
                background-color 0ms,
                $transition-outline;
    outline: 0 solid transparent;
    outline-offset: 2px;
    background-color: $button;
    background-image: none;
    background-size: 1000% 1000%;
    color: $text;
  }

  @if $t == hover {
    transition: $transition,
                border-image $ripple-fade-in-duration $ease-out,
                background-color $ripple-fade-out-duration $ease-out,
                background-size $ripple-fade-out-duration $ease-out,
                background-image $ripple-fade-out-opacity-duration $ease-out;
    background-image: radial-gradient(circle, transparent 10%, transparent 0%);
    background-repeat: no-repeat;
    background-position: center;
    background-size: 1000% 1000%;
    background-color: $overlay-checked;
    outline: 0 solid transparent;
    outline-offset: 2px;
  }

  @if $t == focus {
    outline: 2px solid rgba($primary, 0.35);
    outline-offset: 0;
  }

  @if $t == active {
    transition: $transition,
                background-size 0ms,
                background-image 0ms,
                border 0ms;
    animation: ripple $ripple-fade-in-duration $ease-out forwards;
    background-image: radial-gradient(circle, rgba($primary-left, 0.75) 10%, rgba($primary-right, 0.75) 0%);
    background-size: 0% 0%;
    background-color: transparent;
    color: on($primary);
    outline: 0 solid transparent;
    outline-offset: -2px;
  }

  @if $t == disabled {
    box-shadow: none;
    background-color: $overlay-normal;
    color: $text-disabled;
  }

  @if $t == checked {
    transition: $transition,
                background-size 0ms,
                background-image 0ms,
                background-color 0ms,
                box-shadow 0ms;
    background-image: linear-gradient(to right, $primary-left, $primary-right);
    background-repeat: no-repeat;
    background-position: center;
    background-size: auto;
    background-color: transparent;
    color: on($primary);
  }

  @if $t == checked-hover {
    color: on($primary);
    background-color: transparent;
  }

  @if $t == checked-disabled {
    background-image: none;
    background-color: rgba($primary, 0.5);
    color: on($primary, disabled);
  }

  @if $t == flat-normal {
    transition: $transition;
    outline: 0 solid transparent;
    outline-offset: 2px;
    box-shadow: none;
    background-color: transparent;
    background-image: none;
    color: $text-secondary;
  }

  @if $t == flat-hover {
    transition: $transition,
                border-image $ripple-fade-in-duration $ease-out,
                background-size $ripple-fade-out-duration $ease-out,
                background-image $ripple-fade-out-opacity-duration $ease-out;
    background-color: $overlay-hover;
    background-image: radial-gradient(circle, transparent 10%, transparent 0%);
    background-repeat: no-repeat;
    background-position: center;
    background-size: 1000% 1000%;
    color: $text;
    box-shadow: none;
  }

  @if $t == flat-focus {
    transition: $transition,
                outline $ripple-fade-in-duration $ease-in;
    box-shadow: none;
    color: $text;
    outline: 2px solid $overlay-focus;
    outline-offset: -2px;
  }

  @if $t == flat-active {
    transition: $transition,
                border-image $ripple-fade-in-duration $ease-out,
                background-size 0ms,
                background-image 0ms;
    animation: ripple $ripple-fade-in-duration $ease-out forwards;
    background-color: $overlay-hover;
    background-image: radial-gradient(circle, $overlay-active 10%, transparent 0%);
    background-size: 0% 0%;
    color: $text;
    box-shadow: none;
  }

  @if $t == flat-disabled {
    box-shadow: none;
    background-color: transparent;
    color: $text-secondary-disabled;
  }

  @if $t == flat-checked {
    background-color: $overlay-checked;
    background-image: none;
    color: $text;
    box-shadow: none;
  }

  @if $t == flat-checked-disabled {
    background-color: $overlay-checked;
    color: $text-disabled;
  }
}


@mixin overshoot($p) {
//
// overshoot
//
// $p: position
//
// possible $p values:
// top, bottom, right, left
//

  $_small_gradient_length: 3%;
  $_big_gradient_length: 50%;

  $_small_gradient_size: 100% $_small_gradient_length;
  $_big_gradient_size: 100% $_big_gradient_length;

  @if $p==right or $p==left {
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  $_small_gradient: radial-gradient(farthest-side at $p,
                                    gtkalpha(currentColor, 0.12) 85%,
                                    gtkalpha(currentColor, 0));

  $_big_gradient: radial-gradient(farthest-side at $p,
                                  gtkalpha(currentColor, 0.05),
                                  gtkalpha(currentColor, 0));

  background-image: $_small_gradient, $_big_gradient;
  background-size: $_small_gradient_size, $_big_gradient_size;

  background-repeat: no-repeat;
  background-position: $p;

  background-color: transparent; // reset some properties to be sure to not inherit them somehow
  border: none;                  //
  box-shadow: none;              //
}


@mixin undershoot($p, $c: $frame, $neighbor: false, $style: 'default') {
//
// undershoot
//
// $p: position
// $c: shade color
// $neighbor: use ~ instead of >
//
// possible $p values:
// top, bottom, right, left
//

  $_border_pos: '';
  $_direction: '';
  $_selector: if($neighbor, '~', '>');

  @if $p==top {
    $_direction: bottom;
    $_border_pos: 0 1px;
  } @else if $p==bottom {
    $_direction: top;
    $_border_pos: 0 -1px;
  } @else if $p==left {
    $_direction: right;
    $_border_pos: 1px 0;
  } @else if $p==right {
    $_direction: left;
    $_border_pos: -1px 0;
  } @else {
    @error "Unknown position #{$p}"
  }
  
  #{$_selector} undershoot.#{$p} {
    @if $style == 'compact' {
      box-shadow: inset $_border_pos $c;
    } @else {
      box-shadow: none;
    }

    background: linear-gradient(to $_direction, gtkalpha($c, .75), transparent 4px);
  }
}

@mixin background-shadow($direction, $color) {
  background-image:
  linear-gradient($direction,
                  gtkalpha($color, 0.7),
                  gtkalpha($color, 0.14) 40px,
                  gtkalpha($color, 0) 56px),
  linear-gradient($direction,
                  gtkalpha($color, 0.4),
                  gtkalpha($color, 0.14) 7px,
                  gtkalpha($color, 0) 24px);
}

@mixin transition-shadows($color) {
  > dimming {
    background: $color;
  }
  
  > border {
    background: none;
  }
  
  > shadow {
    min-width: 56px;
    min-height: 56px;
  
    &.left  { @include background-shadow(to right,  $color); }
    &.right { @include background-shadow(to left,   $color); }
    &.up    { @include background-shadow(to bottom, $color); }
    &.down  { @include background-shadow(to top,    $color); }
  }
}
